package com.example.controller;

import com.example.entity.Category;
import com.example.service.CategoryService;
import com.example.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        return ResponseResult.okResult(categoryService.listAllCategory());
    }

    @GetMapping("/list")
    public ResponseResult getList(@PathParam("pageNum") int pageNum, @PathParam("pageSize") int pageSize,@PathParam("name") String name,@PathParam("status") String status){
        return categoryService.getList(pageNum,pageSize,name,status);
    }

    @GetMapping("/{id}")
    public ResponseResult getCategory(@PathVariable Long id){
        return ResponseResult.okResult(categoryService.getById(id));
    }

    @PutMapping()
    public ResponseResult updateCategory(@RequestBody Category category){
        category.setUpdateTime(new Date());
        return ResponseResult.okResult(categoryService.updateById(category));
    }

    @PostMapping()
    public ResponseResult saveCategory(@RequestBody Category category){
        return categoryService.saveCategory(category);
    }

    @DeleteMapping("/{ids}")
    public ResponseResult deleteCategory(@PathVariable List<Long> ids){
        return categoryService.deleteCategory(ids);
    }
}
